package com.gf.pf.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.gf.pf.model.Resource;
import com.gf.pf.model.Ztree;

/**
 * 
 * @description 资源菜单
 * @date   Mar 7, 2015
 */
public interface ResourceMapper {
	
	/**
	 * 根据用户ID查询一级资源菜单SQL
	 */
	final String GET_YIJI_BY_USERID = "SELECT resource_id as id, resource_name, resource_parent_id, resource_url, resource_orderno, resource_isvalid, resource_icon FROM sys_resource WHERE resource_isvalid = 1 and resource_parent_id = '0' and resource_id IN (SELECT resource_id FROM sys_role_resource WHERE role_id IN (SELECT role_id FROM sys_user_role WHERE user_id = #{userId}))";
	
	/**
	 * 根据资源ID查询二级资源菜单SQL
	 */
	final String GET_ERJI_BY_RESOURCEID = "SELECT r.resource_id as id, r.resource_name, r.resource_parent_id, r.resource_url, r.resource_orderno, r.resource_isvalid, r.resource_icon FROM sys_resource r LEFT JOIN sys_role_resource rr ON r.resource_id = rr.resource_id WHERE resource_isvalid = 1 and resource_parent_id = #{parentId} and rr.role_id in (SELECT role_id FROM sys_user_role WHERE user_id = #{userId})";
	
	/**
	 * 保存资源SQL
	 */
	final String SAVE_RESOURCE = "INSERT INTO sys_resource VALUES(#{id}, #{resourceName}, #{resourceParentId}, #{resourceUrl}, #{resourceIcon}, ${resourceOrderno}, ${resourceIsvalid})";
	
	/**
	 * 根据资源ID查询资源信息SQL
	 */
	final String GET_RESOURCE_BY_ID = "SELECT resource_id as id, resource_name, resource_parent_id, resource_url, resource_icon, resource_orderno, resource_isvalid FROM sys_resource WHERE resource_id = #{resourceId}";
	
	/**
	 * 修改资源SQL
	 */
	final String MODIFY_RESOURCE_BY_ID = "UPDATE sys_resource SET resource_name = #{resourceName}, resource_url = #{resourceUrl}, resource_icon = #{resourceIcon}, resource_orderno = ${resourceOrderno}, resource_isvalid = ${resourceIsvalid} WHERE resource_id = #{id}";
	
	/**
	 * 删除资源SQL
	 */
	final String DELETE_RESOURCE_BY_ID = "DELETE FROM sys_resource WHERE resource_id = #{resourceId}";
	
	/**
	 * 查询所有的资源SQL
	 */
	final String GET_RESOURC_ZTREE = "SELECT resource_id as id, resource_parent_id as pId, resource_name as name, resource_icon as icon FROM sys_resource start with resource_parent_id = '-1' connect by prior resource_id = resource_parent_id order by resource_orderno";
	
	/**
	 * 
	 * @describe 根据用户ID查询一级资源菜单
	 * @param userId
	 * @return
	 */
	@Select(GET_YIJI_BY_USERID)
	List<Resource> getYiJiResources(String userId);
	
	/**
	 * 
	 * @description 根据资源ID查询二级资源菜单
	 * @return List<Resource>
	 */
	@Select(GET_ERJI_BY_RESOURCEID)
	List<Resource> getErJiResources(Map<String, String> map);
	
	/**
	 * 
	 * @description 保存资源
	 * @return Integer
	 */
	@Insert(SAVE_RESOURCE)
	Integer saveResource(Resource resource);
	
	/**
	 * 
	 * @description 根据资源ID查询资源信息
	 * @return Resource
	 */
	@Select(GET_RESOURCE_BY_ID)
	Resource getResource(String resourceId);
	
	/**
	 * 
	 * @description 修改资源
	 * @return Integer
	 */
	@Update(MODIFY_RESOURCE_BY_ID)
	Integer modifyResource(Resource resource);
	
	/**
	 * 
	 * @description 删除资源
	 * @return Integer
	 */
	@Delete(DELETE_RESOURCE_BY_ID)
	Integer deleteResource(String resourceId);
	
	/**
	 * 
	 * @description 查询所有的资源
	 * @return List<Ztree>
	 */
	@Select(GET_RESOURC_ZTREE)
	List<Ztree> getResourceZtree();
}
